<template>
  <div>
    <el-form :model="formItem" label-width="120px" size="mini" class="form-view">
      <el-form-item label="供应商名称" prop="name">
        {{ formItem.name }}
      </el-form-item>
      <el-form-item label="供应商地址" prop="address">
        {{ formItem.address }}
      </el-form-item>
      <el-form-item label="供应商联系人" prop="person">
        {{ formItem.person }}
      </el-form-item>
      <el-form-item label="供应商联系电话" prop="phone">
        {{ formItem.phone }}
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { detail } from '@neu/dcam-ui/api/manufacturer'

// 定义一个表单原始项
const formItem = {
  name: '',
  person: '',
  phone: '',
  address: ''
}
export default {
  name: 'ManufacturerViewForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler() {
        if (this.id) {
          this.fetch()
        }
      },
      immediate: true
    }
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    }
  }
}
</script>
